package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.networkdisk.entity.bo.RequestPathBo;
import com.fang.networkdisk.entity.po.TbNetDisRequestPath;
import com.fang.networkdisk.entity.vo.RequestPathVo;
import com.fang.networkdisk.mapper.TbNetDisRequestPathMapper;
import com.fang.networkdisk.service.TbNetDisRequestPathService;
import com.fang.networkdisk.util.NDBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 请求路径，当一个接口需要多个权限的时候需要此表。gateway_request_path_permission_relation 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisRequestPathServiceImpl extends ServiceImpl<TbNetDisRequestPathMapper, TbNetDisRequestPath> implements TbNetDisRequestPathService {

    @Resource
    private TbNetDisRequestPathMapper tbNetDisRequestPathMapper;

    @Override
    public CommonPage queryRequestPath(RequestPathBo requestPathBo) {
        requestPathBo.setOffset((requestPathBo.getCurPage() - 1) * requestPathBo.getPageSize());
        List<RequestPathVo> requestPathVos = this.tbNetDisRequestPathMapper.queryRequestPath(requestPathBo);
        int total = this.tbNetDisRequestPathMapper.queryRequestPathCount(requestPathBo);
        CommonPage result = new CommonPage(requestPathBo.getCurPage(), total, requestPathVos);
        return result;
    }

    @Override
    public TbNetDisRequestPath findRequestPathByUrl(String url) {
        QueryWrapper<TbNetDisRequestPath> wrapper = new QueryWrapper<>();
        wrapper.eq("url", url);
        TbNetDisRequestPath tbNetDisRequestPath = this.tbNetDisRequestPathMapper.selectOne(wrapper);
        return tbNetDisRequestPath;
    }

    @Override
    public int addRequestPath(RequestPathBo requestPathBo) {
        TbNetDisRequestPath db_requestPath = findRequestPathByUrl(requestPathBo.getUrl());
        if (NDBeanUtils.isObjectNotEmpty(db_requestPath)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisRequestPath requestPath = new TbNetDisRequestPath();
        NDBeanUtils.copyListProperties(requestPathBo, requestPath);
        return tbNetDisRequestPathMapper.insert(requestPath);
    }

    @Override
    public int updateRequestPathById(RequestPathBo RequestPathBo) {
        TbNetDisRequestPath requestPath = new TbNetDisRequestPath();
        NDBeanUtils.copyListProperties(RequestPathBo, requestPath);
        return tbNetDisRequestPathMapper.updateById(requestPath);
    }

    @Override
    public int deleteRequestPathById(Integer id, String authorId) {
        return tbNetDisRequestPathMapper.deleteRequestPathById(id, authorId, new Date());
    }
}
